/* -----------------------------------------------------------------------------
 * PROJECT    : lwf_handler
 * FILE       : ExpressionHandler.java
 *
 * HISTORY:
 * Date          Author                        Description
 * ----------------------------------------------------------------------------- 
 * Mar 27, 2012  MyCodeTalks			       Created
 */

package net.lwf.handler.math.expression;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import lwf.handler.IProcessHandler;
import lwf.handler.ProcessHandler;
import lwf.handler.entities.RadioData;
import lwf.handler.helper.DisplayFormatter;
import net.lwf.common.constant.ICommonConstant;
import net.lwf.entity.ExpressionData;
import net.lwf.entity.QuestionInfo;
import net.lwf.handler.util.ExpressionUtil;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;

public class ExpressionHandler extends ProcessHandler implements
		IProcessHandler {
	
	private String type;
	public ExpressionHandler(String grade, String type) {
		this.type = type;
	}

	public DisplayFormatter getDisplayFormatter() {
		return null;
	}

	public String getQuestionDesc() {
		return null;
	}

	@Override
	public QuestionInfo generateSingle() {
		return this.generateSingle(false);
	}

	@Override
	public List<QuestionInfo> generateMultiple() {
		List<QuestionInfo> questions = new ArrayList<QuestionInfo>();
		int nQuestions = getRows() * getColumns();
		for(int i=0; i<nQuestions; i++) {
			QuestionInfo question = generateSingle(true);
			question.setQuestionNumber(i+1);
			questions.add(question);
		}
		return questions;
	}

	@Override
	public void getContent(Document document) throws DocumentException {
	}

	@Override
	public int getRows() {
		return 0;
	}

	@Override
	public int getColumns() {
		return 0;
	}

	@Override
	public boolean isPortrait() {
		return false;
	}

	@Override
	public String getPdfInstructionalText() {
		return null;
	}

	@Override
	public String getPdfNote() {
		return null;
	}
	
	private QuestionInfo generateSingle(boolean isMultiple) {
		QuestionInfo qinfo = new QuestionInfo();
		ExpressionData expdata = null;
		if(this.type.equalsIgnoreCase("compare")) {
			expdata = ExpressionUtil.getComparator();
			if(!isMultiple) {
				qinfo.setRadioAnswerChoices(getRadioAnswers());
			}
		}
		else {
			expdata = ExpressionUtil.getExpressionData();	
		}

		qinfo.setHtmlQuestion(this.generateHtmlQuestion(expdata, this.type));
		qinfo.setAnswer(expdata.getAnswer());
		
		qinfo.setPlaceHolder("Find the missing in the following expression.");

		return qinfo;
	}
	
	private List<RadioData> getRadioAnswers() {
		List<RadioData> rdata = new ArrayList<RadioData>();
		
		rdata.add(new RadioData(ICommonConstant.GREATER_THAN, ICommonConstant.GREATER_THAN));
		rdata.add(new RadioData(ICommonConstant.LESS_THAN, ICommonConstant.LESS_THAN));
		rdata.add(new RadioData(ICommonConstant.EQUAL_TO, ICommonConstant.EQUAL_TO));
		
		Collections.shuffle(rdata);
		
		return rdata;
	}
	
	private String generateHtmlQuestion(ExpressionData expdata, String exptype) {
		StringBuilder sbQBlock = new StringBuilder();
		sbQBlock.append("<table border=\"0\" align=\"center\" >");
			sbQBlock.append("<tr>");
				sbQBlock.append("<td class=\"data\" >");
					sbQBlock.append(expdata.getLhsExpression());
				sbQBlock.append("</td>");
				sbQBlock.append("<td> &nbsp; </td>");
				if(this.type.equalsIgnoreCase("compare")) {
					sbQBlock.append("<td class=\"question\" > ? </td>");
				}
				else {
					sbQBlock.append("<td class=\"data\" > = </td>");
				}
				
				sbQBlock.append("<td> &nbsp; </td>");
				sbQBlock.append("<td class=\"data\" >");
					sbQBlock.append(expdata.getRhsExpression());
				sbQBlock.append("</td>");				
        	sbQBlock.append("</tr>");
        sbQBlock.append("</table>");
		return sbQBlock.toString();	
	}

}
